var app = getApp();
var request = app.request;
var util = require('../../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    data:"",      //请求的数据
    asklist:[],
    ask:null,
    good_id:"",     //评论商品id
    qid:'',         //问题id
    content: "",    //评论内容  
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this;
    // 问答请求
    request.get('/api/user/ask_all', {
      data: { id: options.goods_id, qid: options.qid },
      failRollback: true,
      isShowLoading: false,
      success: function (res) {
        // 时间转换
        // 提问时间
        var ask = res.data.result[0].question;
        ask.addTimeFormat = util.format(ask.createtime, 'yyyy-MM-dd hh:mm');
        // 回答时间
        var result = res.data.result
        for (var i in result){
          for (var j in result[i].comment) {
            result[i].comment[j].time = util.format(result[i].comment[j].createtime, 'yyyy-MM-dd hh:mm');
          }
        }
        that.setData({
          ask: ask,
        });
        // 用户头像
        var setting = app.globalData.setting;
        for (var i in res.data.user){
          if (res.data.user[i].head_pic) {
            if (res.data.user[i].head_pic.indexOf("http") == -1) {
              res.data.user[i].head_pic = setting.url + res.data.user[i].head_pic;
            }
          }else{
            res.data.user[i].head_pic = setting.url + '/template/mobile/rainbow/static/images/user68.jpg';
          }  
        }

        that.setData({ asklist: res.data.result, data: res.data, good_id: options.goods_id,qid: options.qid});
      }
    });
  },
  // textarea值
  bindEquipmentId: function (e) {
    this.setData({
      content: e.detail.value
    })
  },
  // 评论
  a:function(){
    var that = this;
    if (!that.data.content) {
      app.showTextWarining("请输入提问标题");
      return false;
    }
    request.get('/api/user/askall_answer', {
      data: { qid: that.data.qid, id: that.data.goods_id, content: that.data.content },
      failRollback: true,
      isShowLoading: false,
      success: function (res) {
        app.showTextWarining("恭喜您，评论成功!");
        that.setData({
          content:"",
        })
      }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },
  
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})